package com.banshuibao.bsb_android.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.XToast;
import com.banshuibao.bsb_android.util.ZXingUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class InvitCodeActivity extends PermissionBaseActivity implements View.OnClickListener {

    private Bitmap bitmap;
    private AlertDialog alertDialog;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invit_code);
        GetLoginInfo getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
        String firmId = getLoginInfo.getFirmId();
        url = "?firmId="+firmId+"&inviteUserId="+getLoginInfo.getUserId()+"&firmName="+getLoginInfo.getFirmName()+"&inviteUserName="+getLoginInfo.getName();
        initView();
    }

    @Override
    public void successGrantPermission(int requestCode) {
        if(requestCode == 200){
            saveBitmapToLocal("bsb_code.jpg",bitmap,this);
        }
    }

    public void initView(){
        ImageView code_iv = findViewById(R.id.zxing_iv);
        code_iv.setOnClickListener(this);
        Bitmap  bitmap1 = ZXingUtils.createQRImage(Constants.Url.INVITE_MEMBERS+url);
      if(bitmap1 == null){
          XToast.showToast(this,"生成二维码错误，请重试");
          finish();
      }else{
          @SuppressLint("ResourceType") InputStream rawResource = getResources().openRawResource(R.mipmap.icon_round);
         bitmap = ZXingUtils.addLogo(bitmap1, BitmapFactory.decodeStream(rawResource));
          code_iv.setImageBitmap(bitmap);
      }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zxing_iv:
                if(bitmap!=null){
                    showDialog(v);
                }
                break;
        }
    }

    public void showDialog(final View v ){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        alertDialog = builder.setMessage("是否进行保存？").setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.cancel();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                requestUserPermission(v, Manifest.permission.WRITE_EXTERNAL_STORAGE,200);
            }
        }).create();
        alertDialog.show();
    }

   //文件保存的路径
    public static final String FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bsb";

    /**
     * 向本地SD卡写网络图片
     *
     * @param bitmap
     */
    public static void saveBitmapToLocal(String fileName, Bitmap bitmap,Context context) {
        try {
            // 创建文件流，指向该路径，文件名叫做fileName
            File file = new File(FILE_PATH, fileName);
            // file其实是图片，它的父级File是文件夹，判断一下文件夹是否存在，如果不存在，创建文件夹
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                // 文件夹不存在
                fileParent.mkdirs();// 创建文件夹
            }
            // 将图片保存到本地
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                    new FileOutputStream(file));
            XToast.showToast(context,"成功保存至bsb文件夹下");
        } catch (Exception e) {
            XToast.showToast(context,"保存失败"+e.toString());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(alertDialog!=null&&alertDialog.isShowing()){
            alertDialog.cancel();
        }
    }
}
